{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy基础学习\n",
    "## numpy基本属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导包\n",
    "import numpy as np\n",
    "a = np.arange(15).reshape(3, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 5)\n",
      "int64\n",
      "8\n",
      "15\n",
      "几维数组：2\n"
     ]
    }
   ],
   "source": [
    "print(a.shape)\n",
    "print(a.dtype) \n",
    "print(a.itemsize)\n",
    "print(a.size)\n",
    "print(f'几维数组：{a.ndim}') # 几维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## array函数\n",
    "把python的list转换为numpy的array\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n",
      "<class 'numpy.ndarray'>\n",
      "元素类型为：float32\n",
      "每个元素占用的字节数为：4\n",
      "数组的大小为：3\n",
      "数组的形状为：(3,)\n",
      "数组的维数为：1\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3], dtype=np.float32)\n",
    "print(arr1)\n",
    "print(type(arr1))\n",
    "print(f'元素类型为：{arr1.dtype}')\n",
    "print(f'每个元素占用的字节数为：{arr1.itemsize}')\n",
    "print(f'数组的大小为：{arr1.size}')\n",
    "print(f'数组的形状为：{arr1.shape}')\n",
    "print(f'数组的维数为：{arr1.ndim}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n",
      "<class 'numpy.ndarray'>\n",
      "元素类型为：float64\n",
      "每个元素占用的字节数为：8\n",
      "数组的大小为：3\n",
      "数组的形状为：(3,)\n",
      "数组的维数为：1\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.array([1, 2, 3], dtype=np.float64)\n",
    "print(arr2)\n",
    "print(type(arr2))\n",
    "print(f'元素类型为：{arr2.dtype}')\n",
    "print(f'每个元素占用的字节数为：{arr2.itemsize}')\n",
    "print(f'数组的大小为：{arr2.size}')\n",
    "print(f'数组的形状为：{arr2.shape}')\n",
    "print(f'数组的维数为：{arr2.ndim}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## arange()函数\n",
    "arange(start, end, step, dtype), 类似python中的range，包左不包右"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "int32\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "ara = np.arange(0, 10, step=2, dtype=np.int32)\n",
    "print(ara)\n",
    "print(ara.dtype)\n",
    "print(ara.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "int64\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "ara1 = np.arange(0, 10, step=2, dtype=np.int64)\n",
    "print(ara1)\n",
    "print(ara1.dtype)\n",
    "print(ara1.itemsize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建随机数矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.80157214 0.39342165 0.36176779 0.08115425]\n",
      " [0.83856218 0.10571411 0.35949559 0.22418538]\n",
      " [0.17013157 0.76508749 0.06761458 0.15806213]]\n",
      "(3, 4)\n",
      "float64\n",
      "12\n",
      "占用字节：96\n"
     ]
    }
   ],
   "source": [
    "# 创建 0.0-0.1之间的随机数，包左不包右\n",
    "a1 = np.random.rand(3,4)\n",
    "print(a1)\n",
    "print(a1.shape)\n",
    "print(a1.dtype)\n",
    "print(a1.size)\n",
    "print(f'占用字节：{a1.itemsize * a1.size}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 0 4 4]\n",
      " [8 9 9 1]\n",
      " [6 9 8 2]]\n",
      "(3, 4)\n",
      "12\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "# np.random.randint(start, end, size=(行数, 列数))\n",
    "a2 = np.random.randint(0, 10, size=(3,4))\n",
    "print(a2)\n",
    "print(a2.shape)\n",
    "print(a2.size)\n",
    "print(a2.dtype)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.62665227 3.63041647 7.45323602 9.69472809]\n",
      " [3.52895469 3.36766456 9.55326475 0.08657574]\n",
      " [0.50071243 0.23472539 4.53920553 0.37015556]]\n",
      "(3, 4)\n",
      "12\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "# np.random.uniform(start, end, size=(行数, 列数))\n",
    "a3 = np.random.uniform(0, 10, size=(3,4))\n",
    "print(a3)\n",
    "print(a3.shape)\n",
    "print(a3.size)\n",
    "print(a3.dtype)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "float64\n",
      "******************************\n",
      "[[0 0 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "a5 = np.zeros((3,4), dtype=np.float64)\n",
    "print(a5)\n",
    "print(a5.dtype)\n",
    "print('*' * 30)\n",
    "a6 = a5.astype(np.int32)\n",
    "print(a6)\n",
    "print(a6.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建等比数列\n",
    "logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)\n",
    "包含左右边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   1.            5.62341325   31.6227766   177.827941   1000.        ]\n"
     ]
    }
   ],
   "source": [
    "a8 = np.logspace(0, 3, 5) # 生成10个等比数列，底数为10\n",
    "print(a8)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建等差数列\n",
    "linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)\n",
    "包含左右边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.          3.33333333  6.66666667 10.        ]\n"
     ]
    }
   ],
   "source": [
    "a9 = np.linspace(0, 10, 4) # 生成5个等差数列，包含左右边界\n",
    "print(a9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
